package com.blog.login.controller;

import com.blog.login.entity.User;
import com.blog.login.service.UserService;
import com.blog.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin
 * @version 1.0 <br/>
 * <p style="font-family:SimSun;font-weight:bold">时间:<br/>2021/12/8 18:24</p>
 *
 */
@RestController
@Slf4j
@CrossOrigin(origins = "*")
@RequestMapping("/user")
public class RegisterController {
    @Autowired
    UserService userService;
    @PostMapping("/register")
    public ResponseResult register(User user){
        //检查用户名是否已存在
        String userByName = userService.findUserByName(user.getUserName());
        if (!StringUtils.isEmpty(userByName)){
            return ResponseResult.error().message(" 用户名已存在");
        }
        //进行注册流程
        if(userService.registerUser(user)){
            log.info("用户："+user.getUserName()+" 注册成功");
            return ResponseResult.ok().message(" 注册成功");
        }
        log.info("用户："+user.getUserName()+" 注册失败");
        return ResponseResult.error().message(" 注册失败，请稍后重试");
    }
}
